package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;

import com.anhuanjia.tools.eao.bean.EaoAchievements;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;

/**
 * 绩效规则
 * 
 * @author Administrator
 *
 */
@Alias(value = "achievements")
public class AchievementsUI extends UIFacade {

	/**
	 * 查询显示所有绩效
	 */
	@Priv(loginType = LoginType.Member)
	public void findAchievements() {
		ArrayList<EaoAchievements> list = new ArrayList<>();
		DataTable dataTable = new Q("select * from eao_achievements order BY ReportType").fetch();
		for (DataRow dataRow : dataTable) {
			EaoAchievements achievements = new EaoAchievements();
			achievements.setValue(dataRow);
			list.add(achievements);
		}
		EaoAchievements[] result = new EaoAchievements[24];
		for (EaoAchievements achievements : list) {
			result[Integer.parseInt(achievements.getAchievementsId()) - 1] = achievements;
		}
		/*
		 * for (Achievements achievements : result) { System.out.println(achievements);
		 * }
		 */

		this.Response.put("achievements", result);
	}


	/**
	 * 修改绩效值
	 */
	@Priv(loginType = LoginType.Member)
	public void updateAchievements() {
		String achievements_id = this.Request.getString("achievementsId");
		String achievements = this.Request.getString("achievements");
		if (achievements.equals(null)) {
			achievements = "";
		}
		Transaction trans = new Transaction();
		DataTable dt = new Q("select * from eao_achievements where achievementsId = ? ", achievements_id).fetch();
		EaoAchievements old = new EaoAchievements();
		if (dt.getRowCount() > 0) {
			old.setReportType(dt.getString(0, "ReportType"));
			old.setSpecial(dt.getString(0, "Special"));
			old.setDepartment(dt.getString(0, "Department"));
			old.setAchievementsId(achievements_id);
			old.setAchievements(achievements);
			trans.update(old);
			if (trans.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		} else {
			this.Response.put("message", "ID错误");
		}

	}
	// 初始化绩效规则值不为NULL
	/*
	 * @Priv(login = false) public void formartAchievements() { Q q =new
	 * Q("update eao_achievements set achievements='100'", null); Transaction trans
	 * = new Transaction(); trans.add(q); trans.commit(); }
	 */

}
